package com.kly.juc.demo;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author admin
 * @date 2025/4/19
 */
public class RedBagDemo {
    static class RedBag {
        private int total;
        private int money;
        private static ConcurrentHashMap<String, Integer> bagList = new ConcurrentHashMap<>();

        public RedBag(int total, int money) {
            this.total = total;
            this.money = money;
        }

        public void getBagList() {
            int a1 = RandomUtil.randomInt(1, 100);
            int a2 = RandomUtil.randomInt(1, 100 - a1);
            int a3 = 100 - a1 - a2;
            bagList.put("a1", a1);
            bagList.put("a2", a2);
            bagList.put("a3", a3);

        }

        public synchronized void getMoney() {
            if (total > 0) {
                bagList.remove("a1");
                System.out.println(Thread.currentThread().getName() + "抢到了");
                total--;
            } else {
                System.out.println(Thread.currentThread().getName() + "没抢到");
            }
        }
    }

    public static void main(String[] args) {
        RedBag redBag = new RedBag(3, 100);
        redBag.getBagList();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                redBag.getMoney();
            }, "线程" + i).start();
        }

        List<Integer> list = new ArrayList<>();
        Collections.shuffle(list);
    }
}
